package com.cy.store.service.impl;

import com.cy.store.entity.Product;
import com.cy.store.repository.ProductRepository;
import com.cy.store.service.IProductService;
import com.cy.store.service.ex.ProductNotFoundException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class ProductServiceImpl implements IProductService {
    @Autowired
    private ProductRepository productRepository;

    @Override
    public List<Product> findHotList() {
        List<Product> list = productRepository.findHotList();
        for(Product x: list){
            x.setPriority(null);
            x.setCreatedUser(null);
            x.setCreatedTime(null);
            x.setModifiedUser(null);
            x.setModifiedTime(null);
        }
        return list;
    }

    @Override
    public Product findById(Integer id) {
        Product product = productRepository.findById(id).orElseThrow(()->new ProductNotFoundException("尝试访问的商品不存在"));
        //优化
        product.setPriority(null);
        product.setCreatedUser(null);
        product.setCreatedTime(null);
        product.setModifiedUser(null);
        product.setModifiedTime(null);

        return product;
    }
}
